package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.scheduling.dao.SchedulingAuditPipelineSeqDeptPersonDao;
import com.quectel.core.module.scheduling.dto.SchedulingAuditPipelineSeqDeptPersonDto;
import com.quectel.core.module.scheduling.entity.SchedulingAuditPipelineSeqDeptPersonEntity;
import com.quectel.core.module.scheduling.service.SchedulingAuditPipelineSeqDeptPersonService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@DubboService
public class SchedulingAuditPipelineSeqDeptPersonServiceImpl implements SchedulingAuditPipelineSeqDeptPersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAuditPipelineSeqDeptPersonServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_AUDIT_PIPELINE_SEQ_DEPT_PERSON_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_AUDIT_PIPELINE_SEQ_DEPT_PERSON_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingAuditPipelineSeqDeptPersonDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_AUDIT_PIPELINE_SEQ_DEPT_PERSON_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingAuditPipelineSeqDeptPersonDao schedulingAuditPipelineSeqDeptPersonDao;
    @Autowired
    private DeptPersonService deptPersonService;

    @Override
    public void deleteByAuditPipelineSeqId(Long auditPipelineSeqId) {
        LambdaQueryWrapper<SchedulingAuditPipelineSeqDeptPersonEntity> eq =
                new LambdaQueryWrapper<SchedulingAuditPipelineSeqDeptPersonEntity>()
                        .eq(SchedulingAuditPipelineSeqDeptPersonEntity::getAuditPipelineSeqId, auditPipelineSeqId);
        //查询
        List<SchedulingAuditPipelineSeqDeptPersonEntity> entities = schedulingAuditPipelineSeqDeptPersonDao.selectList(eq);

        if (!CollectionUtils.isEmpty(entities)) {
            //删除DB data
            schedulingAuditPipelineSeqDeptPersonDao.delete(eq);
            for (SchedulingAuditPipelineSeqDeptPersonEntity auditPipelineSeqDeptPersonEntity : entities) {
                //删除缓存
                CacheKit.invalidRedisCache(SCHEDULING_AUDIT_PIPELINE_SEQ_DEPT_PERSON_CACHE_BY_ID_FUNC.apply(auditPipelineSeqDeptPersonEntity.getId()));
            }

        }
    }

    @Override
    public List<SchedulingAuditPipelineSeqDeptPersonDto> selectBySeqId(Long auditPipelineSeqId) {
        List<SchedulingAuditPipelineSeqDeptPersonEntity> entities = schedulingAuditPipelineSeqDeptPersonDao.selectList(new LambdaQueryWrapper<SchedulingAuditPipelineSeqDeptPersonEntity>()
                .eq(SchedulingAuditPipelineSeqDeptPersonEntity::getAuditPipelineSeqId, auditPipelineSeqId));
        List<SchedulingAuditPipelineSeqDeptPersonDto> result = CopyUtils.copyList(entities, SchedulingAuditPipelineSeqDeptPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }


    @Override
    public SchedulingAuditPipelineSeqDeptPersonDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_AUDIT_PIPELINE_SEQ_DEPT_PERSON_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingAuditPipelineSeqDeptPersonDto selectById(Long id) {
        SchedulingAuditPipelineSeqDeptPersonEntity entity = schedulingAuditPipelineSeqDeptPersonDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SchedulingAuditPipelineSeqDeptPersonDto.class));
    }

    @Override
    public List<SchedulingAuditPipelineSeqDeptPersonDto> queryList(Map<String, Object> params) {
        List<SchedulingAuditPipelineSeqDeptPersonEntity> list = schedulingAuditPipelineSeqDeptPersonDao.queryList(params);
        List<SchedulingAuditPipelineSeqDeptPersonDto> result = CopyUtils.copyList(list, SchedulingAuditPipelineSeqDeptPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingAuditPipelineSeqDeptPersonDto> queryListWithoutPadding(Map<String, Object> params) {
        List<SchedulingAuditPipelineSeqDeptPersonEntity> list = schedulingAuditPipelineSeqDeptPersonDao.queryList(params);
        return CopyUtils.copyList(list, SchedulingAuditPipelineSeqDeptPersonDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingAuditPipelineSeqDeptPersonDto paddingField(SchedulingAuditPipelineSeqDeptPersonDto dto) {
        if (dto != null) {
            dto.setDeptPersonDto(deptPersonService.selectCacheById(dto.getDeptPersonId()));
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return schedulingAuditPipelineSeqDeptPersonDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingAuditPipelineSeqDeptPersonDto dto) {
        SchedulingAuditPipelineSeqDeptPersonEntity entity = CopyUtils.copyObj(dto, SchedulingAuditPipelineSeqDeptPersonEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingAuditPipelineSeqDeptPersonDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingAuditPipelineSeqDeptPersonDto dto) {
        SchedulingAuditPipelineSeqDeptPersonEntity entity = CopyUtils.copyObj(dto, SchedulingAuditPipelineSeqDeptPersonEntity.class);

        schedulingAuditPipelineSeqDeptPersonDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingAuditPipelineSeqDeptPersonDto dto) {
        SchedulingAuditPipelineSeqDeptPersonEntity entity = CopyUtils.copyObj(dto, SchedulingAuditPipelineSeqDeptPersonEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, schedulingAuditPipelineSeqDeptPersonDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingAuditPipelineSeqDeptPersonDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }


}
